package com.example.leo.mycharmingstudent.a0910;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.leo.mycharmingstudent.R;

public class BroadcastActivity extends AppCompatActivity {

    private MyDynimicsReceiver mDynimicsReceiver;
    private MyDynimicsReceiver2 mDynimicsReceiver2;
    //自定义的广播action
    public final static String MY_ACTION = "android.intent.my.RECEIVE";
    //发送标准（无序）广播
    private Button btnSendStandardBroadcast;
    //发送有序广播
    private Button btnSendOrderBroadcast;
    //发送本地广播
    private Button btnSendLocalBroadcast;
    private LocalReceiver mLocalReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);

        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        btnSendStandardBroadcast = findViewById(R.id.btn_send_standard_broadcast_activity_broadcast);
        btnSendOrderBroadcast = findViewById(R.id.btn_send_order_broadcast_activity_broadcast);
        btnSendLocalBroadcast = findViewById(R.id.btn_send_local_broadcast_activity_broadcast);
    }

    /**
     * 方法当中有set和add需要注意：
     * set是设置，会覆盖
     * add是添加，不覆盖
     *
     * 发送的叫广播，注册的叫广播接收器
     */
    private void initData() {
        //动态注册广播mDynimicsReceiver
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        //添加action，也就是要接收的广播
        intentFilter.addAction(MY_ACTION);
        //设置优先级
        intentFilter.setPriority(0);
        mDynimicsReceiver = new MyDynimicsReceiver();
        registerReceiver(mDynimicsReceiver, intentFilter);

        //动态注册广播mDynimicsReceiver2
        IntentFilter intentFilter1 = new IntentFilter();
        intentFilter1.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter1.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter1.addAction(MY_ACTION);
        intentFilter1.setPriority(-100);
        mDynimicsReceiver2 = new MyDynimicsReceiver2();
        registerReceiver(mDynimicsReceiver2, intentFilter1);

        //注册本地广播接收器，本地广播只能动态注册
        LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
        /*
        注意：以下是思路，不是代码编写流程
        LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
        manager.registerReceiver(testLocalReceiver,testIntentFilter);

        LocalReceiver testLocalReceiver = new LocalReceiver();
        IntentFilter testIntentFilter = new IntentFilter();
        testIntentFilter.addAction();
        testIntentFilter.addAction();
        testIntentFilter.addAction();
        testIntentFilter.addAction();

        */
        IntentFilter localIntentFilter = new IntentFilter();
        localIntentFilter.addAction(Intent.ACTION_SCREEN_ON);
        localIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        localIntentFilter.addAction(MY_ACTION);
        mLocalReceiver = new LocalReceiver();
        manager.registerReceiver(mLocalReceiver,localIntentFilter);
    }

    private void initEvent() {
        btnSendStandardBroadcast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendStandardBroadcast();
            }
        });
        btnSendOrderBroadcast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendOrderBroadcast();
            }
        });
        btnSendLocalBroadcast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendLocalBroadcast();
            }
        });
    }

    /**
     * 发送本地广播
     * 本地广播是重点
     */
    private void sendLocalBroadcast() {
        LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
        Intent intent = new Intent();
        intent.setAction(MY_ACTION);
        /*
        发送本地广播，只有注册本地广播的广播接收器才可以接收到
         */
        manager.sendBroadcast(intent);
    }

    /**
     * 非重点
     * 发送有序广播
     */
    private void sendOrderBroadcast() {
        Intent intent = new Intent();
        intent.setAction(MY_ACTION);
        //发送有序广播
        sendOrderedBroadcast(intent, null);
    }

    /**
     * 发送标准（无序）广播
     */
    private void sendStandardBroadcast() {
        Intent intent = new Intent();
        intent.setAction(MY_ACTION);
        //发送标准（无序）广播
        sendBroadcast(intent);
    }

    /**
     * 广播有注册，就得有解绑注册
     * 为什么呢？
     * 若注册以后，没有及时解绑注册，广播接收器对象会一直在内存中，导致内存泄漏。
     *
     * 内存泄漏：该回收的对象，回收不了。
     * 内存溢出：是一个异常，内存不够用了。
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDynimicsReceiver != null) {
            unregisterReceiver(mDynimicsReceiver);
        }
        if(mDynimicsReceiver2 != null){
            unregisterReceiver(mDynimicsReceiver2);
        }
        if (mLocalReceiver != null) {
            LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
            manager.unregisterReceiver(mLocalReceiver);
        }
    }
}
